package red.book._1._1;

public class BinarySearch {
	public static int rank(int key, int[] a, int lo, int hi, int depth) {
		for(int i = 0; i < depth; i++) {
			System.out.print("	");
		}
		System.out.println("lo=" + lo + ", hi=" + hi);
		depth++;
		if(lo > hi) {
			return -1;
		}
		int mid = lo + (hi -lo) /2; 
		if (key < a[mid]) {
			return rank(key, a, lo, mid-1, depth);
		}else if (key > a[mid]) {
			return rank(key, a, mid + 1, hi, depth);
		}else {
			return mid;
		}
		
	}
	
	public static void main(String[] args) {
		int[] whitelist = new int[] {1, 3, 4, 5, 8};
		int pos = rank(3, whitelist, 0, whitelist.length - 1, 0);
		if (pos == -1) {
			System.out.println("not found!");
		} else {
			System.out.println("position=" + pos);
		}
	}
}
